<?php

namespace app\admin\model;
use Think\Db;
use think\Model;
/**
 * 菜单业务处理
 */
class Menus extends Model
{

    /**
     * 获取菜单
     */
    public function getById($id)
    {
        return (Object) $this->where('id', $id)->find()->toArray();
    }

    /**
     * 新增菜单
     */
    public function add()
    {
        $result = $this->save(input('post.'));
        //日志
        $id             = $this->order("id desc")->find();
        $name = session('my_self')['name'];
        $des = $name."【添加】了编号为【".$id->id."】的菜单";
        add_log(2,session('my_self')['id'], $des);
        if (false !== $result)
        {
            return js_return(1, "添加成功");
        }
        else
        {
            return js_return(0, $this->getError());
        }
    }

    /**
     * 编辑菜单
     */
    public function edit()
    {
        $result = $this->update(input('post.'), 'id=' . input('id'), ['name', 'alias', 'controller', 'action', 'sort']);
        //日志
        $id = input('post.id');
        $name = session('my_self')['name'];
        $des = $name."【修改】了编号为【".$id."】的菜单";
        add_log(2,session('my_self')['id'], $des);
        if (false !== $result)
        {
            return js_return(1, "更新成功");
        }
        else
        {
            return js_return(0, $this->getError());
        }
    }

    /**
     * 删除菜单
     */
    public function del()
    { 
        $id=input('id');
        $result = $this->where( "id={$id} or pid={$id}" )->delete();
        if (false !== $result)
        {
            return [1, "删除成功:".input('id')];
        }
        else
        {
            return [0, $this->getError()];
        }
    }
    /**
     *  获取主菜单
     * @param type $parentId
     * @return type
     */
    public function getMainMenus()
    {
        return (Object) $this->where('pid', 0)->order('sort', 'desc')->select()->toArray();
    }

    /**
     *  获取1,2级菜单
     * @param type $parentId
     * @return type
     */
    public function get2leveMenu()
    {
        $all = $this->getAllMenus();
        $tree = menu2tree($all,'id','pid',0);
        $menus= [];
        foreach ($tree as $val){
            $menus[]= $val;
            if(!empty($val['childs'])){
                foreach ($val['childs'] as $v){
                    $v['name'] = '|----'.$v['name'];
                    $menus[] = $v;
                }
            }
        }

        return $menus;
    }

    public function getAllMenustree()
    {
        $al =  $this->order('sort', 'desc')->select()->toArray();
        return menu2tree($al,'id','pid','0');
    }

    public function getAllMenus()
    {
        return $this->order('sort', 'desc')->select()->toArray();
    }

    public function getNames($key = null)
    {
        static $names = [];
        if (empty($names))
        {
            $rs = $this->getAllMenus();
            foreach ($rs as $v)
            {
                $names[$v['id']] = $v['name'];
            }
        }
        return empty($key) ? $names : ( isset($names[$key]) ? $names[$key] : '');
    }

    public function getNameList($ids)
    {

        if (empty($ids))
            return '';
        $names = $this->getNames();
        $tmp   = [];
        foreach (explode(',', $ids) as $v)
        { 
            if(isset($names[$v]))
            {
                $tmp[] = $names[$v];
            }
        }

        return implode(',', $tmp);
    }


}
